const util = require('../../utils/util.js');
const win = util.getSystemHW();
Component({
  properties: {
    nav: { // 属性名
      type: Array, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
      value: [], // 属性初始值（可选），如果未指定则会根据类型选择一个
      observer: function(newVal, oldVal){} // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串
    },
    navClass: {
      type: String,
      value: ''
    },
    width: {
      type: Number,
      value:win.windowWidth,
      observer: function(newVal, oldVal){}
    },
    initActiveId: {
      type: String,
      value:'0',
      observer: function(newVal, oldVal){
        if(newVal!==null&&newVal!==''){
          this.__initData_evetn(true);
        }
      }
    }
  },
  data: {}, // 私有数据，可用于模版渲染
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  created: function(){},
  attached: function(){

  },
  ready: function(){
    this.triggerEvent("initcomponent",{componentObj:this,componentName:"navbar"});
  },
  moved: function(){},
  detached: function(){
    this.__initData_evetn(false);
  },
  methods: {
    __initData_evetn: function(isAttached){
      var thisObj = this;
      thisObj.setData({
        activeId: isAttached?thisObj.properties.initActiveId:thisObj.data.activeId
      });
    },
    __getCurrentDomSize: function(id,callback){
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      currPage&&currPage.getDomSize&&currPage.getDomSize(id,(res)=>{
        if(callback){
          callback(res);
        }
      },this)
    },
    __onTouchNav_event: function(e){
      var ww = win.windowWidth;
      var c_offestw = e.currentTarget.offsetLeft;
      var cur_offesty = ww/2;
      var thisObj = this;
      var curActivityId = e.currentTarget.dataset.navId;
      var curActivityIndex = e.currentTarget.dataset.navIndex;
      this.__getCurrentDomSize(e.currentTarget.id,(res)=>{
        if(c_offestw<=cur_offesty){//当前domoffestleft不足屏幕宽度一半，不做处理
          thisObj.setData({
            activeId:curActivityId+'',
            activeIndex:curActivityIndex,
            offestLeft:0
          });
        }else if(c_offestw>c_offestw&&res.left>=cur_offesty){//当前dom为滚动的最后一屏且距离屏幕左侧距离大于屏幕宽度一半，不做处理
          thisObj.setData({
            activeId:curActivityId+'',
            activeIndex:curActivityIndex,
          });
        }else{
          var curOffest = (c_offestw-cur_offesty)+res.width/2;
          thisObj.setData({
            activeId:curActivityId+'',
            activeIndex:curActivityIndex,
            offestLeft:curOffest
          });
        };
        thisObj.triggerEvent("switch",{activeId:curActivityId,activeIndex:curActivityIndex});
      })
    },
    __tagtouch_event:function(e){
      var thisObj = this;
      var curIdStr = this.properties.nav[e.navIndex]["category_id"];
      util.getSeletorLoction("the-"+curIdStr,this,(res)=>{
        thisObj.__onTouchNav_event({
          currentTarget:{
            offsetLeft:res.left,
            dataset:res.dataset,
            id:res.id
          }
        })
      })
    },
    __scroll_event: function(e){
      // console.log(e.currentTarget);
    },
    __lower_event: function(e){
      // console.log(e.currentTarget);
    },
    __upper_event: function(e){
      // console.log(e.currentTarget);
    }
  }

})
